package src.main.其他模式.前端控制器模式;

public class FrontControllerPatternDemo {
    public static void main(String[] args) {
        FrontController frontController = new FrontController();
        frontController.dispatchRequest("HOME");
        frontController.dispatchRequest("STUDENT");
    }
}

class HomeView { public void show(){System.out.println("显示主页");}}

class StudentView { public void show(){System.out.println("显示学生页面");}}
/**调度类，分配请求至具体View*/
class Dispatcher {
    private StudentView studentView;
    private HomeView homeView;
    public Dispatcher(){
        studentView = new StudentView();
        homeView = new HomeView();
    }

    public void dispatch(String request){
        if(request.equalsIgnoreCase("STUDENT")){
            studentView.show();
        }else{
            homeView.show();
        }
    }
}

class FrontController {
    private Dispatcher dispatcher;
    public FrontController(){dispatcher = new Dispatcher();}
    private boolean isAuthenticUser(){
        System.out.println("用户身份验证成功");
        return true;
    }
    private void trackRequest(String request){
        System.out.println("请求的页面: " + request);
    }
    public void dispatchRequest(String request){
        //记录每一个请求
        trackRequest(request);
        //对用户进行身份验证
        if(isAuthenticUser()){dispatcher.dispatch(request);}
    }
}